package com.ling.minio.service;
 

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.credentials.AssumeRoleProvider;
import io.minio.credentials.Credentials;
import io.minio.credentials.StaticProvider;
import io.minio.errors.*;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
 
public class Test03 {
    //服务所在ip地址和端口
    public static final String ENDPOINT = "http://127.0.0.1:9090/";
  
    //mc的用户名
    public static final String ACCESS_KEY_COMPANY = "ling007";
    //mc的密码
    public static final String SECRET_KEY_COMPANY = "mi123456";
    //aws服务端点
//    public static final String REGION = "cn-north-1";
    //上传的bucket名
    public static final String BUCKET = "minio-upload";
    //授权策略，允许访问名为bucket的桶的目录
    public static final String ROLE_ARN = "arn:aws:s3:::minio-upload/*";
//    public static final String ROLE_SESSION_NAME = "anysession";
    //定义策略，可进行二次限定
    public static final String POLICY_GET_AND_PUT = "{\n" +
            " \"Version\": \"2012-10-17\",\n" +
            " \"Statement\": [\n" +
            "  {\n" +
            "   \"Effect\": \"Allow\",\n" +
            "   \"Action\": [\n" +
            "    \"s3:GetObject\",\n" +
            "    \"s3:GetBucketLocation\",\n" +
            "    \"s3:PutObject\"\n" +
            "   ],\n" +
            "   \"Resource\": [\n" +
            "    \"arn:aws:s3:::minio-upload/*\"\n" +
            "   ]\n" +
            "  }\n" +
            " ]\n" +
            "}";
    public static void main(String[] args) {
        try {
            int durationSeconds = 360000;//秒
            //创建签名对象
            AssumeRoleProvider provider = new AssumeRoleProvider(
                    ENDPOINT,
                    ACCESS_KEY_COMPANY,
                    SECRET_KEY_COMPANY,
                    durationSeconds,//默认3600秒失效，设置小于这个就是3600，大于3600就实际值
                    POLICY_GET_AND_PUT,
                    null,
                    ROLE_ARN,
                    null,
                    null,
                    null);
            /**
             * 打印provider签名属性
             */
            System.out.println("sessionToken="+provider.fetch().sessionToken());
            System.out.println("accessKey="+provider.fetch().accessKey());
            System.out.println("secretKey="+provider.fetch().secretKey());
            System.out.println("isExpired="+provider.fetch().isExpired());
 
            Credentials credentials = provider.fetch();
            String sessionToken=provider.fetch().sessionToken();
            String accessKey=provider.fetch().accessKey();
            String secretKey=provider.fetch().secretKey();
 
 
 
 
            StaticProvider staticProvider = new StaticProvider(credentials.accessKey(), credentials.secretKey(), credentials.sessionToken());
            MinioClient minioClient = MinioClient.builder().endpoint(ENDPOINT).credentialsProvider(staticProvider).build();
            File file = new File("C:\\Users\\A\\Pictures\\600 (2).jpg");
            //这个objectName的值是经过上面的policy授权的
            String objectName = "600 (2).jpg";
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                minioClient.putObject(PutObjectArgs.builder().bucket(BUCKET)
                        .object(objectName)
                        .contentType("image/jpg")
                        .stream(fileInputStream, fileInputStream.available(), -1).build());
 
//                File file2 = new File("C:\\Users\\zhaohao\\Pictures\\Camera Roll\\微信截图_20210308150013 - 副本.png");
//                //这个objectName的值是经过上面的policy授权的
//                String objectName2 = "mx/pic2.jpg";
//                FileInputStream fileInputStream2 = new FileInputStream(file2);
//                minioClient.putObject(PutObjectArgs.builder().bucket(BUCKET)
//                        .object(objectName2)
//                        .contentType("image/jpg")
//                        .stream(fileInputStream2, fileInputStream2.available(), -1).build());
            } catch (Exception e) {
                e.printStackTrace();
            }
 
 
            /*//使用签名获取mc对象
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(ENDPOINT)
                    .credentialsProvider(provider)
                    .build();
            String filename = "github-docker-compose.yml";
            try {
                //对象流，获取文件
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\dockerFile\\" + filename));
                //使用mc对象上传文件
                minioClient.putObject(PutObjectArgs.builder()
                        //桶名和aws服务端点
                        .bucket(BUCKET).region(REGION)
                        //前缀和对象名
                        .object("mx/" + filename)
                        .stream(bis, bis.available(), -1)
                        .build());
                System.out.println("文件上传成功！！");
            } catch (ErrorResponseException e) {
                e.printStackTrace();
            } catch (InsufficientDataException e) {
                e.printStackTrace();
            } catch (InternalException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (InvalidResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (XmlParserException e) {
                e.printStackTrace();
            }*/
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
 
    }
}